package com.wanmait.server.controller;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.wanmait.common.response.BaseResponse;
import com.wanmait.common.response.StatusCode;
import com.wanmait.server.util.ShiroUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;

/**
   *@Author 羽
   *@Date 2020/12/8  19:50
   *@Version 1.0
**/
@Controller
public class LoginController extends AbstractController {

    @Autowired
    private Producer producer;

    /**
     * TODO
     * @author 羽
     * @date 2020/12/11 12:09
     * 验证码
     **/
    @RequestMapping("verificationCode")
    public void verificationCode(HttpServletResponse response) throws Exception{
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        //生成文字验证码
        String text = producer.createText();
        //生成图片验证码
        BufferedImage image = producer.createImage(text);
        //保存到shiro session
        ShiroUtil.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY, text);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);

        System.out.println("验证码："+text);
    }

    /**
     * 点击登录，对用户名和密码进行验证
     * @Author 羽
     * @Date 2020/12/8  20:12
     * @Version 1.0
     **/
    @RequestMapping("loginVerification")
    @ResponseBody
    public BaseResponse<String> loginVerification(String username, String password,String captcha) {
        log.info("用户名{}，密码{}，验证码{}", username, password,captcha);
        //登陆
        //校验验证码
        String kaptcha=ShiroUtil.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
        if (!kaptcha.equals(captcha)){
            return new BaseResponse(StatusCode.InvalidCode);
        }
        try {
            Subject subject = SecurityUtils.getSubject();
            if(!subject.isAuthenticated()){
                UsernamePasswordToken token = new UsernamePasswordToken(username,password);
                subject.login(token);
            }

        } catch (UnknownAccountException e) {
            //不存在
            return new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }catch (IncorrectCredentialsException e) {
            //密码不匹配
            return new BaseResponse(StatusCode.AccountPasswordNotMatch);
        }catch (LockedAccountException e) {
            //被锁定
            return new BaseResponse(StatusCode.AccountHasBeenLocked);
        }catch (AuthenticationException e) {
            //认证失败
            return new BaseResponse(StatusCode.AccountValidateFail);
        }

        return new BaseResponse<String>(StatusCode.Success);
    }

    /**
     * 退出登录
     */
    @RequestMapping("logout")
    public String logout(){
        //销毁当前的shiro的用户session
        ShiroUtil.logout();
        return "redirect:login";
    }





}
